package com.sskj.gocoinapp.http;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.m.bean.AppVersionBean;
import com.sskj.gocoinapp.bean.BannerBean;
import com.sskj.gocoinapp.bean.GuideListBean;
import com.sskj.gocoinapp.bean.LoginBean;
import com.sskj.gocoinapp.bean.MiningQPBean;
import com.sskj.gocoinapp.bean.MiningTips;
import com.sskj.gocoinapp.bean.Stock;
import com.sskj.gocoinapp.bean.SummaryBean;
import com.sskj.gocoinapp.bean.TradeBean;
import com.sskj.gocoinapp.bean.ZixunListBean;
import com.sskj.level.bean.GonggaoBean;
import com.sskj.level.bean.SocketSendBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.APKVersionCodeUtils;

import java.util.List;
import java.util.concurrent.Executor;

import okhttp3.WebSocket;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService {


    /**
     * 行情数据
     *
     * @param code 标识符
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getProduct(String code) {
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PRODUCT)
                .params("code", code);
    }


    /**
     * 获取k线数据
     *
     * @param goodsType 分时数据标示
     * @param code      币种
     * @return
     */
    public GetRequest<HttpData<List<Stock>>> getKData(String goodsType, String code) {
        return OkGo.<HttpData<List<Stock>>>get(HttpConfig.BASE_URL + HttpConfig.GET_K_DATA)
                .params("goodsType", goodsType)
                .params("code", code);
    }


    /**
     * 获取轮播图及公告
     *
     * @return
     */
    public GetRequest<HttpData<List<BannerBean>>> getBanner(String type) {
        return OkGo.<HttpData<List<BannerBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_BANNER)
                .params("type", type)
                ;
    }


    /**
     * 发送验证码（注册）
     *
     * @param mobile 手机号
     * @param type   1注册 （2 重置 3 安全验证 4 资金密码设置 5 提币）
     * @return
     */
    public GetRequest<HttpData> sendSms(String mobile, String type) {
        return OkGo.<HttpData>get(HttpConfig.BASE_URL + HttpConfig.SEND_SMS)
                .params("mobile", mobile)
                .params("type", type);
    }

    /**
     * 发送邮件
     *
     * @param email 邮箱号
     * @return
     */
    public PostRequest<HttpData> sendEmail(String email,String type) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", email)
                .params("type", type)
                ;
    }

    /**
     * 注册
     *
     * @param mobile 手机号/邮箱号
     * @param code   验证码
     * @param opwd   密码
     * @param opwd1  重复密码
     * @param tjuser 推荐码/邀请码
     * @return
     */
    public PostRequest<HttpData> register(String mobile, String code, String opwd, String opwd1, String tjuser) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.REGISTER)
                .params("mobile", mobile)
                .params("code", code)
                .params("opwd", opwd)
                .params("opwd1", opwd1)
                .params("is_app", 1)
                .params("tjuser", tjuser);
    }

    /**
     * 登录
     *
     * @param mobile 手机号/邮箱号
     * @param opwd   密码
     * @return
     */
    public PostRequest<HttpData<LoginBean>> login(String mobile, String opwd) {
        return OkGo.<HttpData<LoginBean>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                .params("mobile", mobile)
                .params("opwd", opwd);
    }

    /**
     * 重置登录密码
     *
     * @param mobile 手机号/邮箱号
     * @param opwd   密码
     * @param opwd1  确认登录密码
     * @param code   验证码
     * @return
     */
    public PostRequest<HttpData> resetLoginPwd(String mobile, String opwd, String opwd1, String code) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.RESET_LOGIN_PWD)
                .params("mobile", mobile)
                .params("opwd", opwd)
                .params("opwd1", opwd1)
                .params("code", code);
    }


    public WebSocketObserver pushCoinType() {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response) {
                webSocket.send(new Gson().toJson(new SocketSendBean("all")));
            }
        };
    }

    public WebSocketObserver pushCoinTrade(String code) {
        return new WebSocketObserver(HttpConfig.WS_FIVE_TRADE_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response) {
                webSocket.send(new Gson().toJson(new SocketSendBean(code)));
            }
        };
    }

    /**
     * 获取气泡
     */

    public GetRequest<HttpData<List<MiningQPBean>>> getMiningNum() {
        return OkGo.<HttpData<List<MiningQPBean>>>get(HttpConfig.BASE_URL + HttpConfig.APP_MINING_QP);
    }

    /**
     * 获取天天挖矿提示文本
     */
    public GetRequest<HttpData<MiningTips>> getMiningTips() {
        return OkGo.<HttpData<MiningTips>>get(HttpConfig.BASE_URL + HttpConfig.APP_MINING_TIPS);
    }

    /**
     * 领取已经成熟的泡泡
     */
    public PostRequest<HttpData> successQp(String type, String pid) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.APP_MINING_GET)
                .params("type", type)
                .params("pid", pid);
    }

    /**
     * 获取累计收益
     */

    public GetRequest<HttpData<MiningQPBean>> getIcome() {
        return OkGo.<HttpData<MiningQPBean>>get(HttpConfig.BASE_URL + HttpConfig.APP_MINING_INCOME);
    }

    /**
     * 获取原力明细
     */

    public GetRequest<HttpData<List<MiningQPBean>>> getYLRecord() {
        return OkGo.<HttpData<List<MiningQPBean>>>get(HttpConfig.BASE_URL + HttpConfig.APP_MINING_RECORD);
    }


    public GetRequest<HttpData<SummaryBean>> getSummary(String code) {
        return OkGo.<HttpData<SummaryBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_SUMMARY)
                .params("code", code)
                ;
    }

    public GetRequest<HttpData<List<WSFiveBean>>> getDeepData(String code) {
        return OkGo.<HttpData<List<WSFiveBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_DEEP)
                .params("code", code)
                ;
    }

    public PostRequest<HttpData<List<TradeBean>>> getAllTrade(String code) {
        return OkGo.<HttpData<List<TradeBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_ALL_TRADE)
                .params("code", code)
                ;
    }

    public PostRequest<HttpData<PageBean<ZixunListBean>>> getZixunList(String page) {
        return OkGo.<HttpData<PageBean<ZixunListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_ZIXUN_LIST)
                .params("p", page)
                .params("s", 20)
                ;
    }

    public PostRequest<HttpData<PageBean<GuideListBean>>> getGuideList(String page) {
        return OkGo.<HttpData<PageBean<GuideListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_GUIDE_LIST)
                .params("p", page)
                .params("s", 15)
                ;
    }

    public PostRequest<HttpData> checkGoogle(String code) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_GOOGLE)
                .params("dyGoodleCommand", code)
                ;
    }

    public WebSocketObserver pushCoinFiveDeep(String type) {
        return new WebSocketObserver(HttpConfig.WS_FIVE_DEEP_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response) {
                webSocket.send(new Gson().toJson(new SocketSendBean(type)));
            }
        };
    }

    public GetRequest<HttpData<GonggaoBean>> getGonggao() {
        return OkGo.<HttpData<GonggaoBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_GONGGAO)
                ;
    }

    public PostRequest<HttpData<AppVersionBean>> getVersion() {
        return OkGo.<HttpData<AppVersionBean>>post(HttpConfig.BASE_URL + HttpConfig.VERSION)
                .params("version", APKVersionCodeUtils.getVerName(MyAppLication.getApplication()))
                .params("type", "1");


    }

    public PostRequest<HttpData> checkCode(String account, String code) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_CODE)
                .params("mobile", account)
                .params("code", code);

    }

    public PostRequest<HttpData<PageBean<GuideListBean>>> getGonggaoList(String page) {
        return OkGo.<HttpData<PageBean<GuideListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_GONGGAO_LIST)
                .params("p", page)
                .params("size", 15)
                ;
    }

    public GetRequest<HttpData<GuideListBean>> getGonggaoInfo(String id) {
        return OkGo.<HttpData<GuideListBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_GONGGAO_INFO)
                .params("id", id)
                ;
    }

    public PostRequest<HttpData<LoginBean>> loginCode(String account, String code) {
        return OkGo.<HttpData<LoginBean>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN_CODE)
                .params("mobile", account)
                .params("code", code)
                ;
    }
}